var selectedSeedRow='',selectedBizRow='';
var isInit = true, isInitTable = true;

$(function () {
    initImportExcelPage(downLoadExcelTemp, doImportExcelData, '导入埋点信息');
});

function downLoadExcelTemp() {
    var url = basePath + 'pvuv/statSeedBiz/downloadExcelTemp?t=' + new Date().getTime();
    doDownTempExcel(url);
}

function doImportExcelData() {
    var url = basePath + 'pvuv/statSeedBiz/excelImportStatInfo?t=' + new Date().getTime();
    doImportExcel(url, doRefresh);
}

function doRefresh() {
    doRefreshSeed();
    doRefreshBiz();
}

function doRefreshSeed() {
    selectedSeedRow = '';
    var isStat = $('#isStat').val();
    var seedType = $('#seedType').val();
    var seedName = $.trim($('#seedName').val());
    $('#seedTable').bootstrapTable('refresh', {
        url: basePath + 'pvuv/statSeedBiz/pageFindSeedData?t=' + new Date().getTime(),
        query: {
            name : seedName,
            isStat : isStat,
            seedType : seedType
        },
    });
}

function doRefreshBiz() {
    selectedBizRow = '';
    var isStat = $('#isStat').val();
    var seedName = $.trim($('#seedName').val());
    var bizName = $.trim($('#bizName').val());
    var isStatBiz = $('#isStatBiz').val();
    var seedType = selectedSeedRow ? selectedSeedRow.seedType : $('#seedType').val();
    var seedId = selectedSeedRow ? selectedSeedRow.seedId : '';
    $('#bizTable').bootstrapTable('refresh', {
        url: basePath + 'pvuv/statSeedBiz/pageFindBizData?t=' + new Date().getTime(),
        query: {
            seedName : seedName,
            name : bizName,
            isStat : isStatBiz,
            isStatSeed : isStat,
            seedType : seedType,
            seedId : seedId,
        },
    });
}

function getLeftParams(params) {
    var isStat = $('#isStat').val();
    var seedType = $('#seedType').val();
    var seedName = $.trim($('#seedName').val());
    var pageSize = params.pageSize;
    var pageNo = params.pageNumber;
    if (isInit) {
        isInit = false;
        pageSize = searchInfo.pageSize ? searchInfo.pageSize : params.pageSize;
        pageNo = searchInfo.pageNo ? searchInfo.pageNo : params.pageNumber;
    }
    return {
        name : seedName,
        isStat : isStat,
        seedType : seedType,
        pageSize : pageSize,
        pageNo : pageNo
    };
}

function getRightParams(params) {
    var isStat = $('#isStat').val();
    var seedType = $('#seedType').val();
    var seedId = selectedSeedRow ? selectedSeedRow.seedId : searchInfo.selectedSeedId;
    var seedName = $.trim($('#seedName').val());
    var bizName = $.trim($('#bizName').val());
    var isStatBiz = $('#isStatBiz').val();
    var pageSize = params.pageSize;
    var pageNo = params.pageNumber;
    return {
        seedName : seedName,
        name : bizName,
        isStat : isStatBiz,
        isStatSeed : isStat,
        seedType : seedType,
        seedId : seedId,
        pageSize : pageSize,
        pageNo : pageNo
    };
}

function openAddSeedPage() {
    openSeedEditPage('add', 0, '');
}

function openEditSeedPage() {
    if (!selectedSeedRow) {
        toastr.error('请先选择一条记录再进行编辑');
        return;
    }
    openSeedEditPage('edit', selectedSeedRow.id, selectedSeedRow.seedId);
}

function openSeedEditPage(command, id, seedId) {
    var bizOptions=$('#bizTable').bootstrapTable('getOptions', null);
    var seedOptions=$('#seedTable').bootstrapTable('getOptions', null);
    var searchParam = $('#searchForm').serialize().replace(/&/g, ',') +
        ",selectedSeedId=" + seedId +
        ",pageNoRight=" + bizOptions.pageNumber +
        ",pageSizeRight=" + bizOptions.pageSize +
        ",pageNoLeft=" + seedOptions.pageNumber +
        ",pageSizeLeft=" + seedOptions.pageSize +
        ",isPvExist=" + searchInfo.isPvExist +
        ",isUvExist=" + searchInfo.isUvExist +
        ",isStatExist=" + searchInfo.isStatExist;
    window.location.href = basePath + 'pvuv/statSeedBiz/editSeedPage?editType=' + command +
        '&id=' + id + '&t=' + new Date().getTime() +
        "&searchParam="+searchParam;
}

function doDeleteSeed() {
    if (!selectedSeedRow) {
        toastr.error('请先选择一条记录再进行删除');
        return;
    }
    if (selectedSeedRow.isStat == 'Y') {
        toastr.error('处于监控状态时无法删除');
        return;
    }
    var postData = {
        seedId : selectedSeedRow.seedId,
        seedType : selectedSeedRow.seedType,
    };
    $.ajax({
        type: 'post',
        url: basePath + 'pvuv/statSeedBiz/countBizBySeed?t=' + new Date().getTime(),
        dataType: 'json',
        contentType: "application/json",
        data: JSON.stringify(postData),
        success : function (data) {
            if(data && data.result){
                var strHtml = '确认是否删除？提示：删除后后台不再对该项进行监控';
                swalToastr('操作提示', strHtml, '', doSeedDel);
            }else if(data&&!data.result){
                toastr.error(data.message);
            }else{
                toastr.error('修改失败');
            }
        },
        error: function (request) {
            delayToastr('系统错误');
        }
    });
}

function doSeedDel() {
    var postData = {
        id : selectedSeedRow.id,
    };
    $.ajax({
        type: 'post',
        url: basePath + 'pvuv/statSeedBiz/deleteSeed?t=' + new Date().getTime(),
        dataType: 'json',
        contentType: "application/json",
        data: JSON.stringify(postData),
        success : function (data) {
            if(data && data.result){
                toastr.success('删除成功');
                doRefreshSeed();
            }else if(data&&!data.result){
                toastr.error(data.message);
            }else{
                toastr.error('删除失败');
            }
        },
        error: function (request) {
            delayToastr('系统错误');
        }
    });
}

function openImportPage() {
    showImportExcelPage();
}

function changeSeedState() {
    if (!selectedSeedRow) {
        toastr.error('请先选择一条记录再进行操作');
        return;
    }
    var strHtml = '是否确定取消监控统计项【'+selectedSeedRow.name+'】';
    if (selectedSeedRow.isStat == 'N') {
        strHtml = '是否确定监控统计项【'+selectedSeedRow.name+'】';
    }
    swalToastr('操作提示', strHtml, '', doSeedChangeState);
}

function doSeedChangeState() {
    var isStat = selectedSeedRow.isStat == 'N' ? 'Y' : 'N';
    var str = selectedSeedRow.isStat == 'N' ? '监控' : '停止监控';
    var postData = {
        id : selectedSeedRow.id,
        isStat : isStat,
    };
    $.ajax({
        type: 'post',
        url: basePath + 'pvuv/statSeedBiz/changeSeedState?t=' + new Date().getTime(),
        dataType: 'json',
        contentType: "application/json",
        data: JSON.stringify(postData),
        success : function (data) {
            if(data && data.result){
                toastr.success(str + '【' + selectedSeedRow.name + '】成功');
                doRefreshSeed();
            }else if(data&&!data.result){
                toastr.error(data.message);
            }else{
                toastr.success(str + '【' + selectedSeedRow.name + '】失败');
            }
        },
        error: function (request) {
            delayToastr('系统错误');
        }
    });
}

function openAddBizPage() {
    if (!selectedSeedRow) {
        toastr.error('请先选择所属统计项再新增统计子项');
        return;
    }
    if (selectedSeedRow.isLeaf == 'Y') {
        toastr.error('末节点不能添加统计子项');
        return;
    }
    openBizEditPage('add', 0, selectedSeedRow.seedType, selectedSeedRow.seedId, selectedSeedRow.name, selectedSeedRow.seedId);
}

function openEditBizPage() {
    if (!selectedBizRow) {
        toastr.error('请先选择一条记录后再进行编辑');
        return;
    }
    var selectedSeedId = selectedSeedRow ? selectedSeedRow.seedId : '';
    openBizEditPage('edit', selectedBizRow.id, selectedBizRow.seedType, selectedBizRow.seedId, selectedBizRow.seedName, selectedSeedId);
}

function openBizEditPage(command, id, seedType, seedId, seedName, selectedSeedId) {
    var bizOptions=$('#bizTable').bootstrapTable('getOptions', null);
    var seedOptions=$('#seedTable').bootstrapTable('getOptions', null);
    var searchParam = $('#searchForm').serialize().replace(/&/g, ',') +
        ",selectedSeedId=" + selectedSeedId +
        ",pageNoRight=" + bizOptions.pageNumber +
        ",pageSizeRight=" + bizOptions.pageSize +
        ",pageNoLeft=" + seedOptions.pageNumber +
        ",pageSizeLeft=" + seedOptions.pageSize +
        ",isPvExist=" + searchInfo.isPvExist +
        ",isUvExist=" + searchInfo.isUvExist +
        ",isStatExist=" + searchInfo.isStatExist;
    window.location.href = basePath + 'pvuv/statSeedBiz/editBizPage?editType=' + command + '&id=' + id +
        '&seedType=' + seedType + '&seedId=' + seedId + '&seedName=' + seedName + '&t=' + new Date().getTime()+
        '&searchParam=' + searchParam;
}

function doDeleteBiz() {
    if (!selectedBizRow) {
        toastr.error('请先选择一条记录再进行删除');
        return;
    }
    if (selectedBizRow.isStat == 'Y') {
        toastr.error('处于监控状态时无法删除');
        return;
    }
    var strHtml = '确认是否删除？提示：删除后后台不再对该项进行监控';
    swalToastr('操作提示', strHtml, '', doBizDel);
}

function doBizDel() {
    var postData = {
        id : selectedBizRow.id,
    };
    $.ajax({
        type: 'post',
        url: basePath + 'pvuv/statSeedBiz/deleteBiz?t=' + new Date().getTime(),
        dataType: 'json',
        contentType: "application/json",
        data: JSON.stringify(postData),
        success : function (data) {
            if(data && data.result){
                toastr.success('删除成功');
                doRefreshBiz();
            }else if(data&&!data.result){
                toastr.error(data.message);
            }else{
                toastr.error('删除失败');
            }
        },
        error: function (request) {
            delayToastr('系统错误');
        }
    });
}

function changeBizState() {
    if (!selectedBizRow) {
        toastr.error('请先选择一条记录再进行操作');
        return;
    }
    var strHtml = '是否确定取消监控统计子项【'+selectedBizRow.name+'】';
    if (selectedBizRow.isStat == 'N') {
        strHtml = '是否确定监控统计子项【'+selectedBizRow.name+'】';
    }
    swalToastr('操作提示', strHtml, '', doBizChangeState);
}

function doBizChangeState() {
    var isStat = selectedBizRow.isStat == 'N' ? 'Y' : 'N';
    var str = selectedBizRow.isStat == 'N' ? '监控' : '停止监控';
    var postData = {
        id : selectedBizRow.id,
        isStat : isStat,
    };
    $.ajax({
        type: 'post',
        url: basePath + 'pvuv/statSeedBiz/changeBizState?t=' + new Date().getTime(),
        dataType: 'json',
        contentType: "application/json",
        data: JSON.stringify(postData),
        success : function (data) {
            if(data && data.result){
                toastr.success(str + '【' + selectedBizRow.name + '】成功');
                doRefreshBiz();
            }else if(data&&!data.result){
                toastr.error(data.message);
            }else{
                toastr.success(str + '【' + selectedBizRow.name + '】失败');
            }
        },
        error: function (request) {
            delayToastr('系统错误');
        }
    });
}

function formatIsStat(value, row, index) {
    return value == 'Y' ? '是' : '否';
}

function initCheck(value, row, index) {
    if (isInitTable && row.seedId == searchInfo.selectedSeedId) {
        setTimeout(function(){
            $("#seedTable > tbody > tr[data-index='" +index+"']").addClass("success");
        },200);
        isInitTable = false;
        selectedSeedRow = row;
    }
    return value;
}

//------------------------------------------------------------------------列表选中事件
//左边选中事件
$("#seedTable").on("click-row.bs.table",function(e,row,ele){
    $("#seedTable .success").removeClass("success");
    $(ele).addClass("success");
    selectedSeedRow = row;
    if ($('#_btnChangeState')) {
        if (row.isStat == 'Y') {
            $('#_btnChangeState').html('停止监控');
        } else {
            $('#_btnChangeState').html('监控');
        }
    }
    doRefreshBiz();
});
//右边选中事件
$("#bizTable").on("click-row.bs.table",function(e,row,ele){
    $("#bizTable .success").removeClass("success");
    $(ele).addClass("success");
    if ($('#_btnBizChangeState')) {
        if (row.isStat == 'Y') {
            $('#_btnBizChangeState').html('停止监控');
        } else {
            $('#_btnBizChangeState').html('监控');
        }
    }
    selectedBizRow = row;
});
